package com.night.attendance.web;

import com.alibaba.excel.EasyExcel;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.night.attendance.DistException;
import com.night.attendance.DistNotFoundException;
import com.night.attendance.excelHandler.AttendanceRecordHandler;
import com.night.attendance.mapper.AttendanceConfigMapper;
import com.night.attendance.service.AttendanceConfigService;
import com.night.attendance.service.StudentAttendanceService;
import com.night.common.entity.ResultVo;
import com.night.common.entity.administration.vo.CourseMangerQueryVo;
import com.night.common.entity.attendance.*;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author Dark-Morning
 * 2022/10/18 19:15
 */
@RestController
@RequestMapping("api/attendance")
public class AttendanceController {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    StudentAttendanceService studentAttendanceService;

    @Autowired
    AttendanceConfigService attendanceConfigService;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @PostMapping("addAttendance")
    public ResultVo<?> addAttendance(@RequestBody AttendanceConfigDO attendanceConfigDO){
        attendanceConfigService.saveOne(attendanceConfigDO);
        return ResultVo.SUCCESS;
    }

    @PostMapping("addReason")
    public ResultVo<?> addReason(@RequestBody StudentAttendanceDO studentAttendanceDO){
        // TODO: 2022/10/27 从jwt获取学生id
        studentAttendanceService.updateStudentAttendance(studentAttendanceDO);
        return ResultVo.SUCCESS;
    }


    @PostMapping("studentAttendance")
    public ResultVo<?> studentAttendance(@RequestBody AttendanceInfoDO attendanceInfoDO){
        // TODO: 2022/10/27 从jwt获取学生id
        try {
            studentAttendanceService.attendance(attendanceInfoDO);
            return ResultVo.SUCCESS;
        }catch (Exception e) {
            if (e instanceof DistNotFoundException) {
                return new ResultVo<>(800, "距离超出");
            } else if (e instanceof DistException) {
                return new ResultVo<>(800,"签到已经结束");
            }
            e.printStackTrace();
            return ResultVo.FAIL;
        }
    }

    @PostMapping("listAttendanceConfig")
    public ResultVo<?> listAttendanceConfig(@RequestBody AttendanceConfigQueryDO attendanceConfigQueryDO){
        // TODO: 2022/10/27 从jwt获取教师id
        attendanceConfigService.listAttendanceConfig(attendanceConfigQueryDO);
        return new ResultVo<>(attendanceConfigQueryDO.getPageBean());
    }

    @PostMapping("listAttendance")
    public ResultVo<?> listAttendance(@RequestBody StudentAttendanceQueryDO studentAttendanceQueryDO){
        // TODO: 2022/10/27 从jwt获取学生id

         studentAttendanceService.listStudentAttendance(studentAttendanceQueryDO);
         return new ResultVo<>(studentAttendanceQueryDO.getPageBean());
    }

    @PostMapping("downloadAttendanceRecord")
    public void downloadAttendanceRecord (@RequestBody StudentAttendanceQueryDO studentAttendanceQueryDO, HttpServletResponse response) throws IOException {
        List<StudentAttendanceDO> data =  studentAttendanceService.listAttendanceRecord(studentAttendanceQueryDO);
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), StudentAttendanceDO.class).registerWriteHandler(new AttendanceRecordHandler()).autoCloseStream(Boolean.FALSE).sheet("模板")
                    .doWrite(data);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.writeValue(response.getWriter(), ResultVo.ERROR);
            response.getWriter().flush();
            response.getWriter().close();
        }
    }
}
